package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

@RestController
@Api(tags = "菜品相关的接口")
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {

    public static final String PATTERN = "dish_*";

    @Autowired
    private DishService dishService;

    @Resource
    private RedisTemplate redisTemplate;


    /*
     * 菜品的分页条件查询
     * */
    @ApiOperation("分页条件查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页条件查询菜品:{}", dishPageQueryDTO);
        PageResult page = dishService.page(dishPageQueryDTO);
        return Result.success(page);
    }

    /*
     * 新增菜品及其口味
     * */
    @ApiOperation("新增菜品及其口味")
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品及其口味:{}", dishDTO);
        dishService.add(dishDTO);
        deleteCache(PATTERN);
        return Result.success();
    }

    /*
     * 根据id查询菜品及其口味
     * */
    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("根据id查询菜品:{}", id);
        DishVO dishVO = dishService.queryById(id);
        return Result.success(dishVO);
    }

    /*
     * 修改菜品及其口味
     * */
    @ApiOperation("修改菜品及其口味")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品及其口味:{}", dishDTO);
        dishService.update(dishDTO);
        deleteCache(PATTERN);
        return Result.success();
    }

    /*
     * 批量删除菜品及其口味
     * */
    @DeleteMapping
    @ApiOperation("批量删除菜品及其口味")
    public Result deleteBatch(@RequestParam List<Long> ids) {
        log.info("删除菜品及其口味:{}", ids);
        dishService.deleteBatch(ids);
        deleteCache(PATTERN);
        return Result.success();
    }

    /*
     * 设置菜品的销售状态
     * */
    @ApiOperation("设置菜品的销售状态")
    @PostMapping("/status/{status}")
    public Result enableOrDisable(@PathVariable Integer status, @RequestParam Long id) {
        log.info(status == 1 ? "起售菜品id:{}" : "停售菜品id:{}", id);
        dishService.enableOrDisable(id, status);
        deleteCache(PATTERN);
        return Result.success();
    }

    /*
     * 根据分类id查询菜品
     * */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result getByCategoryId(Long categoryId) {
        log.info("根据分类id来查询:{}", categoryId);
        List<Dish> dishes = dishService.queryByCategoryId(categoryId);
        return Result.success(dishes);
    }

    private void deleteCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
